package cn.kgc.shiroday03.service.impl;

import cn.kgc.shiroday03.commons.CodeMsg;
import cn.kgc.shiroday03.commons.ResponseResult;
import cn.kgc.shiroday03.dto.RolePageDTO;
import cn.kgc.shiroday03.entity.Role;
import cn.kgc.shiroday03.entity.RolePers;
import cn.kgc.shiroday03.entity.UserRole;
import cn.kgc.shiroday03.mapper.RoleMapper;
import cn.kgc.shiroday03.mapper.RolePersMapper;
import cn.kgc.shiroday03.mapper.UserRoleMapper;
import cn.kgc.shiroday03.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;


    @Autowired
    private RolePersMapper rolePersMapper;


    @Override
    public ResponseResult getRolePage(RolePageDTO rolePageDTO) {

        // 分页查询  role
        Page<Role> rolePage = new Page<>(rolePageDTO.getPage(), rolePageDTO.getLimit());
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String roleName = rolePageDTO.getRoleName();
        // 构建查询条件 实现接口的复用  搜索和查询所有接口的复用
        lambdaQueryWrapper.like((!ObjectUtils.isEmpty(roleName)&& !"".equals(roleName)),Role::getRoleName,roleName);
        roleMapper.selectPage(rolePage,lambdaQueryWrapper);

        List<Role> records = rolePage.getRecords();
        long total = rolePage.getTotal();

        return new ResponseResult(CodeMsg.SUCCESS,total,records);
    }

    @Override
    public ResponseResult saveOrUpdateRole(Role role) {

        //  1. 判定用户是更新还是添加    id
        Integer roleId = role.getRoleId();
        if(ObjectUtils.isEmpty(roleId)){
            //添加
            LambdaQueryWrapper<Role> lambda = new LambdaQueryWrapper<>();
            lambda.eq(Role::getComments,role.getComments())
                    .or()
                    .eq(Role::getRoleName,role.getRoleName());
            List<Role> roles = roleMapper.selectList(lambda);
            if(roles.size()==0){
                roleMapper.insert(role);
            }else {
                 return new  ResponseResult(CodeMsg.ROLE_EXIST);
            }
        }else {
            roleMapper.updateById(role);
        }
        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(String ids) {

        //处理前端用户传入的参数   ids
        String[] split = ids.split(",");
        ArrayList<Integer> intIds = new ArrayList<>();
        for (String id : split){
            intIds.add(Integer.valueOf(id));
        }
        // 判定删除的角色id  是否被占用
        LambdaQueryWrapper<UserRole> lambda = new LambdaQueryWrapper<>();
        lambda.in(UserRole::getRoleId,intIds);
        List<UserRole> userRoles = userRoleMapper.selectList(lambda);

        if(userRoles.size()==0){
            roleMapper.deleteBatchIds(intIds);
            return new ResponseResult(CodeMsg.SUCCESS);
        }

        return new ResponseResult(CodeMsg.ROLE_HASUSED);
    }

    @Override
    public ResponseResult selectPermissionByRoleId(Integer roleId) {


        LambdaQueryWrapper<RolePers> lambda = new LambdaQueryWrapper<>();
        lambda.eq(RolePers::getRoleId,roleId);
        List<RolePers> rolePers = rolePersMapper.selectList(lambda);
        List<Integer> pids = rolePers.stream().map(rp -> rp.getPerId()).collect(Collectors.toList());

        return new ResponseResult(CodeMsg.SUCCESS,null,pids);
    }

    @Override
    public ResponseResult addRolePermission(Integer roleId, String permissionIds) {

        //  1.根据角色id  删除 角色权限信息   role_per
        LambdaQueryWrapper<RolePers> lambda = new LambdaQueryWrapper<>();
        lambda.eq(RolePers::getRoleId,roleId);
        rolePersMapper.delete(lambda);

        // 2 添加当前角色的最新权限信息

        String[] split = permissionIds.split(",");

        for (String id : split) {
            RolePers rolePers = new RolePers();
            rolePers.setRoleId(roleId);
            rolePers.setPerId(Integer.parseInt(id));
            rolePersMapper.insert(rolePers);
        }

        return new ResponseResult(CodeMsg.SUCCESS);
    }
}




